<?
$file = $_GET['name'] . 'query.kml';
header('Content-type: application/vnd.google-earth.kml+xml');
header('Content-disposition: attachment; filename='. $file);

require_once($_SERVER["DOCUMENT_ROOT"] . '/db/db.php');
$db = new DBConnection('dbname=assclap_realty port=5432 user=assclap_assclap password=fibo1235');

$sql = "SELECT * FROM {$_GET['name']}";
$Query = $db->CreateQuery($sql);

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>

<kml xmlns="http://earth.google.com/kml/2.1">
 <Document>
  <name>PolygonStyle.kml</name>
  <open>1</open>
  <Style id="examplePolyStyle">
    <LabelStyle>
      <color>7fffaaff</color>
      <scale>1.5</scale>
    </LabelStyle>
    <LineStyle>
      <color>eeDDDDDD</color>
      <colorMode>normal</colorMode>
      <width>2</width>
    </LineStyle>
	<PolyStyle>
      <color>7fcccccc</color>
      <colorMode>random</colorMode>
	  <fill>1</fill>                     <!-- boolean -->
	  <outline>1</outline>               <!-- boolean -->
    </PolyStyle>
  </Style>
<?

// Iterates through the results, creating one Placemark for each row.
while ($row = $Query->FetchObject())
{
	// Creates a Placemark and append it to the Document.
	?>
  <Placemark id="placemark-<? echo $row->name; ?>">
    <name><? echo htmlentities($row->name); ?></name>
    <description><? echo $row->description; ?></description>
    <styleUrl>#examplePolyStyle</styleUrl>
    <? echo "    " . $row->kml . "\n"; ?>
  </Placemark>
    <?
}
// End XML file
?>
 </Document>
</kml>